{
 "cells": [
  {
   "cell_type": "code",
   "id": "initial_id",
   "metadata": {
    "collapsed": true,
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.268806Z",
     "start_time": "2025-09-04T14:52:35.260863Z"
    }
   },
   "source": [
    "import numpy as np\n",
    "\n",
    "a = np.array([1,2,3])\n",
    "b = np.array([4,5,6])\n",
    "print(a + b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)"
   ],
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[5 7 9]\n",
      "[-3 -3 -3]\n",
      "[ 4 10 18]\n",
      "[0.25 0.4  0.5 ]\n"
     ]
    }
   ],
   "execution_count": 40
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.293460Z",
     "start_time": "2025-09-04T14:52:35.287005Z"
    }
   },
   "cell_type": "code",
   "source": [
    "c = [1,2,3]\n",
    "d = [4,5,6]\n",
    "print(c + d) # [1.13模块编程.12package, 2, 3, 4, 5, 6] 把两个列表加起来\n",
    "print(c * 2) # [1.13模块编程.12package, 2, 3, 1.13模块编程.12package, 2, 3]\n",
    "print(np.add(c,d)) # [5 7 9]"
   ],
   "id": "7a49a6cf3bf140a4",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1, 2, 3, 4, 5, 6]\n",
      "[1, 2, 3, 1, 2, 3]\n",
      "[5 7 9]\n"
     ]
    }
   ],
   "execution_count": 41
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.319977Z",
     "start_time": "2025-09-04T14:52:35.312767Z"
    }
   },
   "cell_type": "code",
   "source": [
    "e = []\n",
    "for i in range(10):\n",
    "    e.append(i)\n",
    "print(e)\n",
    "f = np.arange(10)\n",
    "print(f)\n",
    "\n",
    "g = [1,2,3,None]\n",
    "h = [4,5,6,7]\n",
    "print(g)\n",
    "# print(np.add(g,h)) # TypeError: unsupported operand type(s) for +: 'int' and 'NoneType'"
   ],
   "id": "18d5194af2457399",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]\n",
      "[0 1 2 3 4 5 6 7 8 9]\n",
      "[1, 2, 3, None]\n"
     ]
    }
   ],
   "execution_count": 42
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.344363Z",
     "start_time": "2025-09-04T14:52:35.334402Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 多维数组---矩阵 类似于一维矩阵\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([[4,5,6],[7,8,9]])\n",
    "print(a + b)\n",
    "print(a - b)\n",
    "print(a * b)\n",
    "print(a / b)\n",
    "print(np.add(a,b))\n",
    "\n",
    "print('*'.center(50,'-'))\n",
    "print(a & b) # 矩阵元素与矩阵元素进行按位与运算\n",
    "\n",
    "print(a + 3) # 矩阵元素加3\n",
    "print(a ** 2) # 矩阵元素平方"
   ],
   "id": "2f032ef5c704b46e",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5  7  9]\n",
      " [11 13 15]]\n",
      "[[-3 -3 -3]\n",
      " [-3 -3 -3]]\n",
      "[[ 4 10 18]\n",
      " [28 40 54]]\n",
      "[[0.25       0.4        0.5       ]\n",
      " [0.57142857 0.625      0.66666667]]\n",
      "[[ 5  7  9]\n",
      " [11 13 15]]\n",
      "------------------------*-------------------------\n",
      "[[0 0 2]\n",
      " [4 0 0]]\n",
      "[[4 5 6]\n",
      " [7 8 9]]\n",
      "[[ 1  4  9]\n",
      " [16 25 36]]\n"
     ]
    }
   ],
   "execution_count": 43
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.366134Z",
     "start_time": "2025-09-04T14:52:35.356833Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 广播机制 1.13模块编程.12package.获取形状 2.比较形状 3.广播机制 4.计算\n",
    "a = np.array([[1,2,3],[4,5,6]])\n",
    "b = np.array([[4],[7]])\n",
    "print(a + b)\n",
    "\n",
    "a = np.array([1,2,3]) # 1X3\n",
    "b = np.array([[4],[5],[6]]) # 3X1\n",
    "print(a + b) # 3X3"
   ],
   "id": "f3af87ec9677ea35",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 5  6  7]\n",
      " [11 12 13]]\n",
      "[[5 6 7]\n",
      " [6 7 8]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "execution_count": 44
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.383659Z",
     "start_time": "2025-09-04T14:52:35.379351Z"
    }
   },
   "cell_type": "code",
   "source": [
    "# 不具备广播机制\n",
    "# a = np.array([1.13模块编程.12package,2,3]) # 1X3\n",
    "# b = np.array([4,5]) # 1X2\n",
    "# print(a + b)"
   ],
   "id": "26ee6cb1b615d891",
   "outputs": [],
   "execution_count": 45
  },
  {
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-09-04T14:52:35.420952Z",
     "start_time": "2025-09-04T14:52:35.413929Z"
    }
   },
   "cell_type": "code",
   "source": [
    "a = np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "b = np.array([[10,11,12],[13,14,15],[16,17,18]])\n",
    "print(a @ b)"
   ],
   "id": "af5d07cf3d822527",
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 84  90  96]\n",
      " [201 216 231]\n",
      " [318 342 366]]\n"
     ]
    }
   ],
   "execution_count": 46
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
